package com.example.deliveryapplication

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.deliveryapplication.entity.BearingModel
import com.example.deliveryapplication.entity.Client
import com.example.deliveryapplication.entity.SelectString
import com.example.deliveryapplication.sql.BearingModelDao
import com.example.deliveryapplication.sql.ClientDao
import com.example.deliveryapplication.sql.PlanItemDao
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class BearingModelViewModel:BaseViewModel() {
    private val mResult = MutableLiveData<List<BearingModel>>()
    val result: LiveData<List<BearingModel>> get() = mResult
    fun getAll(clientDao:BearingModelDao){
        launch {
            val clients = clientDao.loadAllChart()
            mResult.postValue(clients)
        }
    }


    private val mDeleteResult = MutableLiveData<Int>()
    val deleteResult: LiveData<Int> get() = mDeleteResult
    fun deleteModel(planItemDao:PlanItemDao,bearingModelDao: BearingModelDao,modelId:String){
        launch {
            val list = planItemDao.loadPlanItemByModelId(modelId)
            if(list.isEmpty()){

                val bean=bearingModelDao.loadModelById(modelId)
                val result=bearingModelDao.deleteModel(bean)
                mDeleteResult.postValue(result)
            }else{
                mDeleteResult.postValue(-1)
            }

        }
    }

    private val mUpdateResult = MutableLiveData<Int>()
    val updateResult: LiveData<Int> get() = mUpdateResult
    fun updateModel(modelDao:BearingModelDao,bean:BearingModel){
        launch {
            val result = modelDao.updateModel(bean)
            mUpdateResult.postValue(result)
        }
    }
}